package com.dreambook.android.ui;

import android.app.ProgressDialog;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.dreambook.android.R;
import com.dreambook.android.db.DreamBookColumns;
import com.dreambook.android.db.DreamBookQuery;
import com.dreambook.android.model.DreamBook;
import com.dreambook.android.util.Config;

public class ListDreembookActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
	// private String type;
	private ProgressDialog progress;
	private ListView listView;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.list_dream_book);
		listView = (ListView) findViewById(R.id.list_dreambook);

		Bundle bun = getIntent().getExtras();

		if (bun != null) {
			// type = getIntent().getStringExtra(Config.TYPE);

			getSupportLoaderManager().initLoader(DreamBookQuery._TOKEN, bun, this);

		}

	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
		switch (id) {
		case DreamBookQuery._TOKEN:
			progress = ProgressDialog.show(this, getString(R.string.loading), getString(R.string.please_wait), true, false);
			return new CursorLoader(ListDreembookActivity.this, DreamBook.CONTENT_URI, DreamBookQuery.PROJECTION, DreamBookColumns.DREAMBOOK_TYPE.getName()
					+ "=?", new String[] { bundle.getString(Config.TYPE) }, DreamBook.NAME_SORT);
		default:
			return null;
		}
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		switch (loader.getId()) {
		case DreamBookQuery._TOKEN:
			progress.dismiss();
			ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor,
					new String[] { DreamBookColumns.NAME.getName() }, new int[] { android.R.id.text1 });

			listView.setAdapter(adapter);

			// mCursor = cursor;
			break;
		default:
			break;
		}
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		// TODO Auto-generated method stub

	}
}
